package com.cheyun.netsalev3.view

import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HistoryTagAdapter
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.dao.AppDatabase
import com.cheyun.netsalev3.databinding.ActivitySearchBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.view.home.CustomerItemFragment
import com.cheyun.netsalev3.viewmodel.CustomerItemFragmentViewModel
import com.cheyun.netsalev3.viewmodel.SearchActivityViewModel
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagFlowLayout

class SearchActivity : BaseActivity<ActivitySearchBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_search
    }

    private var from = ""
    private var listFragmet = ArrayList<Fragment>()

    var nkey = ""

    override fun initViewModel(): ViewModel? {
        if(intent.hasExtra("nkey")){
            nkey = intent.getStringExtra("nkey")
        }
        if(intent.hasExtra("from")){
            from = intent.getStringExtra("from")
        }
//        var factory = InjectorUtils.getSearchActivityViewModelFactory(nkey,from, this)
//        return ViewModelProviders.of(this, factory).get(SearchActivityViewModel::class.java)
        var nkeys = arrayListOf(
                nkey
        )
        var hashMap = HashMap<String, String>()
        val factory = InjectorUtils.getCustomerItemFragmentViewModelFactory(nkeys, hashMap)
        return ViewModelProviders.of(this, factory).get(CustomerItemFragmentViewModel::class.java)
    }

    override fun initData() {
        var database=  AppDatabase.getInstance(this).getSearchDao()
        binding.viewModel?.initSearchRepository(database)

        binding.myEdit.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                binding.viewModel?.search(nkey)
                true
            } else
                false
        }

//        binding.viewModel?.searchlist?.observe(this, Observer { list ->
//            adapter.submitList(list)
//        })
        binding.viewModel?.searchlist?.observe(this, Observer {
            historyTagAdapter?.setData(it)
        })

//        binding.viewModel?.carlist?.observe(this, Observer { list ->
//            carAdapter.submitList(list)
//        })
    }

//    private lateinit var adapter: MyAdapter
    //new
    var historyTagAdapter:HistoryTagAdapter? = null
//    private lateinit var carAdapter: MyAdapter


    override fun initView() {
//        adapter = MyAdapter(R.layout.search_item, binding.viewModel)
//        binding.recyclerView.adapter = adapter

//        carAdapter = MyAdapter(R.layout.series_search_item, binding.viewModel)
//        binding.quickRecyclerView.adapter = carAdapter
//
//        binding.txtAdvance.visibility = if(from.equals("allocated")) View.GONE else View.VISIBLE



        historyTagAdapter = HistoryTagAdapter(this,null )
        binding.recyclerView.setAdapter(historyTagAdapter)

        binding.recyclerView.setOnTagClickListener(object : TagFlowLayout.OnTagClickListener{
            override fun onTagClick(view: View?, position: Int, parent: FlowLayout?): Boolean {
//                customerEditTagAdapter?.notifyDataChanged()
                binding.viewModel?.searchItem(nkey,position)

                return true
            }
        })

        listFragmet.add(CustomerItemFragment.newInstance(nkey))
        val pagerAdapter = HomePageAdapter(supportFragmentManager, listFragmet)
        binding.viewPager.adapter = pagerAdapter
    }
}
